﻿using CodeMan.DbOther;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;

using Volo.Abp.ObjectMapping;
using System.Linq;
using Microsoft.Extensions.Logging;
using BootstrapBlazor.Components;
using CodeMan.Domain.ClassGens;
using CodeMan.Application.Contracts.ClassGens;
using CodeMan.Domain.Shared.Enums;
using CodeMan.Domain.Shared;

namespace CodeMan.ClassGens
{
    public class OtherDBAppService : IOtherDBAppService
    {
        private IOtherDBRepository _repository;
        private IObjectMapper ObjectMapper;
        private readonly ILogger _logger;


        public OtherDBAppService(
            IOtherDBRepository repository,
            IObjectMapper objectMapper,
            ILogger<OtherDBAppService> logger)
        {
            _repository = repository;
            ObjectMapper = objectMapper;
            _logger = logger;
        }

        public async Task<List<TableEntity>> GetAllTables()
        {
            var dd = new List<TableEntity>();
            try
            {
                dd = await _repository.GetAllTables("");
                return dd;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetAllTables");
            }
            return dd;
        }

        public async Task<IReadOnlyList<TreeItem>> GetTreeItemsAsync(string tkey)
        {
            var result = new List<TreeItem>();

            var resultAll = await _repository.GetAllTables(tkey);

            var resultAllGroupBy = resultAll.GroupBy(a => a.Name);

            foreach (var item in resultAllGroupBy)
            {
                var currDto = new ClassGenDTOCreateUpdate() { Name = item.Key, FromType = FromTypeEnum.otherDB };
                //类、表
                var ptreeitem = new TreeItem() { Text = currDto.Name, Key = "table_1", Tag = currDto,  IsDisabled = false, HasChildNode = true };

                if (tkey.IsNotNullOrWhiteSpace())
                {
                    var resultClass = new List<TreeItem>();
                    resultClass.Add(new TreeItem() { Text = currDto.Desc ?? currDto.Name, IsDisabled = true });
                    resultClass.Add(new TreeItem() { Text = $"Namespace:{currDto.Namespace}", IsDisabled = true });
                    resultClass.Add(new TreeItem() { Text = $"Type:{currDto.Type}", IsDisabled = true });

                    //字段,属性
                    var attrTreimte = new TreeItem() { Text = "字段明细", Key = "attr", IsDisabled = true };
                    foreach (var l in item)
                    {
                        AttrTypeEnum attrTypeEnum = CodeManConsts.DicMSSqlAttType.GetValueOrDefault(l.AType, AttrTypeEnum.sstring);
                        var ilst = new ClassAttributeDTO() { IsKey = l.IsKey, ADesc = l.ADesc ?? l.AName, AName = l.AName, defaultValue = l.defaultValue, Length = l.Length, AType = attrTypeEnum, IsAutoGenerate = true };
                        var currlist = new TreeItem() { Text = ilst.AName, Key = $"{ currDto.Name}_{ilst.AName}", Tag = ilst, IsDisabled = true };

                        currlist.AddItem(new TreeItem() { Text = ilst.ADesc, IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"IsKey:{ilst.IsKey}", IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"Type:{ilst.AType}", IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"Length:{ilst.Length}", IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"defaultValue:{ilst.defaultValue}", IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"IsAutoGenerate:{ilst.IsAutoGenerate}", IsDisabled = true });
                        currlist.AddItem(new TreeItem() { Text = $"IsShow:{ilst.IsShow}", IsDisabled = true });

                        attrTreimte.AddItem(currlist);
                    }

                    resultClass.Add(attrTreimte);
                    return resultClass;
                }

                result.Add(ptreeitem);
            }
            return result;
        }

        public async Task<ResponseMsg> SetConnectionString(string connstr)
        {
            var dd = new ResponseMsg();
            try
            {
                var result = await _repository.SetConnectionString(connstr);
                return dd;
            }
            catch (Exception ex)
            {
                dd.Status = -1;
                dd.Message = ex.Message;
                _logger.LogError(ex, "SetConnectionString");
            }
            return dd;
        }
    }
}
